/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.common;

import java.util.Calendar;

/**
 * 任务记录, 包含任务性质, 总数, 成功数, 失败数
 * 
 * @author huangmy
 * @date   2008-3-16
 */
public class TaskRepository {
	private long wait; // 未处理数
    private long success; // 成功数
    private long errorPlatform;  // 平台处理失败数
    private long errorService;  // 服务处理失败数
    private Property property; // 性质
    private Calendar lastModifyDate = Calendar.getInstance();
    
    public static enum Property {
    	SEND("SEND"), // 发出
    	RECV("RECV"); // 收到
    	
    	private String name; // 名称
    	
    	private Property(String name) {
    		this.name = name;
    	}
    	
    	@Override
    	public String toString() {
    		return name;
    	}
    }
    
    public TaskRepository(Property property) {
    	this.property = property;
    }
    
    /**
     *  任务总数 = 成功数 + 平台失败数 + 服务失败数 + 未处理数
     *  
     * @return
     */
    public synchronized long getTotal() {
    	return success + errorPlatform + errorService + wait;
    }
    
    /**
     * 成功数
     * 
     * @return
     */
	public synchronized long getSuccess() {
		return success;
	}
    
	/**
	 * 增加成功数
	 *  
	 */
	public synchronized void incrSuccess() {
		maybeReset();
		
		if (this.wait > 0) {
			this.success ++;
			this.wait --;
		}
	}

	/**
	 * 平台处理失败数
	 * 
	 * @return
	 */
	public synchronized long getErrorPlatform() {
		return errorPlatform;
	}

	/**
	 * 增加平台处理失败数
	 * 
	 */
	public synchronized void incrErrorPlatform() {
		maybeReset();
		
		if (this.wait > 0) {
			this.wait --;
			this.errorPlatform ++;
		}
	}

	/**
	 * 服务处理失败数
	 * 
	 * @return
	 */
	public synchronized long getErrorService() {
		return errorService;
	}

	/**
	 * 增加服务处理失败数
	 * 
	 */
	public synchronized void incrErrorService() {
		maybeReset();
		
		if (this.wait > 0) {
			this.errorService ++;
			this.wait --;
		}
	}
	
	/**
	 * 处理失败总数
	 * 
	 * @return
	 */
	public synchronized long getError() {
		return errorPlatform + errorService;
	}

	/**
	 * 任务性质
	 * 
	 * @return
	 */
	public synchronized Property getProperty() {
		return property;
	}

	/**
	 * 未处理数
	 * 
	 * @return
	 */
	public synchronized long getWait() {
		return wait;
	}

	/**
	 * 增加未处理
	 * 
	 */
	public synchronized void incrWait() {
		maybeReset();
		
		this.wait ++;
	}
	
	private synchronized void maybeReset() {
		Calendar date = Calendar.getInstance();
		if (date.get(Calendar.YEAR) != this.lastModifyDate.get(Calendar.YEAR)
				|| date.get(Calendar.MONTH) != this.lastModifyDate.get(Calendar.MONTH)
				|| date.get(Calendar.DAY_OF_MONTH) != this.lastModifyDate.get(Calendar.DAY_OF_MONTH)) {
			reset();
		}
		
		this.lastModifyDate = date;
	}
	
	/**
	 * 重置计数
	 */
	private synchronized void reset() {
		this.wait = 0;
		this.errorPlatform = 0;
		this.errorService = 0;
		this.success = 0;
	}

	public synchronized Calendar getLastModifyDate() {
		return lastModifyDate;
	}
}
